<?php
require_once "string_helper.php";
function _check_file($file) {
    if (!file_exists($file) || !is_readable($file)) {
        return FALSE;
    }
    return TRUE;
}

function path_join() {
    $args = func_get_args();
    $paths = array();
    foreach ($args as $arg) {
        $paths = array_merge($paths, (array)$arg);
    }
    $paths = array_map(function($p, $k) {
        if ($k == 0) {
            return rtrim($p, "/");
        } else {
            return trim($p, "/");
        }
    }, $paths, array_keys($paths));
    return join('/', $paths);
}

function get_arc_file_type($file) {
    if (!_check_file($file)) {
        return NULL;
    }
    $command = "file $file";
    exec($command, $data);
    $output_line = $data[0];
    if (strstr($output_line, "gzip")) {
        return "gzip";
    } else if (strstr($output_line, "Zip")) {
        return "zip";
    }
}

function get_file_abs_path($file) {
    if (!_check_file($file)) {
        return NULL;
    }
    $command = "readlink -f $file";
    exec($command, $data);
    return $data[0];
}

function extract_file($src, $dest_path) {
    if (!_check_file($src)) {
        return FALSE;
    }
    if (!is_writeable($dest_path)) {
        return FALSE;
    }
    $type = get_arc_file_type($src);
    switch ($type) {
        case 'gzip' :
            $command = "tar zxvf $src -C $dest_path";
            break;
        case 'zip' :
            $command = "unzip -o $src -d $dest_path";
            break;
    }
    exec($command, $data);
    dbg($data);
}
function force_download($filename = '', $data = '')
    {
        if ($filename == '' OR $data == '')
        {
            return FALSE;
        }

        // Try to determine if the filename includes a file extension.
        // We need it in order to set the MIME type
        if (FALSE === strpos($filename, '.'))
        {
            return FALSE;
        }

        // Grab the file extension
        $x = explode('.', $filename);
        $extension = end($x);

        // Set a default mime if we can't find it
        $mime = 'application/octet-stream';

        // Generate the server headers
        if (strpos($_SERVER['HTTP_USER_AGENT'], "MSIE") !== FALSE)
        {
            header('Content-Type: "'.$mime.'"');
            header('Content-Disposition: attachment; filename="'.$filename.'"');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header("Content-Transfer-Encoding: binary");
            header('Pragma: public');
            header("Content-Length: ".strlen($data));
        }
        else
        {
            header('Content-Type: "'.$mime.'"');
            header('Content-Disposition: attachment; filename="'.$filename.'"');
            header("Content-Transfer-Encoding: binary");
            header('Expires: 0');
            header('Pragma: no-cache');
            header("Content-Length: ".strlen($data));
        }

        exit($data);
    }
?>